/**
 * Created by flyger on 2017/3/7 0007.
 *
 * 实现类似jsp中<%=basePath%>的功能，获取项目根目录有助于url的填写
 *
 * 使用方法，用类似引用jQuery的方法引入本插件
 *
 * 定义一个全局的变量，然后的ready方法内部掉用本方法，获得basePath
 *
 * //调用方法
 var basePath;;

 $(document).ready(
 function() {
        basePath=basePath();
        alert（basePath）;
//结果如下
//http://127.1.0.1:8088/yangcangclub/
    });

 */

function basePath(){
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //获取项目的basePath   http://localhost:8080/ems/
    var basePath=localhostPath+projectName+"/";
    return basePath;
};


/**
 * 获得url 中的参数
 *
 * @param str
 * @returns {*}
 * @constructor
 * 使用方法：  var access_token = GetUrl("access_token");
 *
 */
function GetUrl(str){
    var LocString=String(window.document.location.href);
    var rs=new RegExp("(^|)"+str+"=([^&]*)(&|$)","gi").exec(LocString),tmp;
    if(tmp=rs)return tmp[2];
    return "";
}
/*
* 判断是否为微信浏览器
* */
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)!="micromessenger") {
    // alert("非微信浏览器访问");
    // window.location.href=basePath()+"v1/user/wx/login";
}
/*
* 微信js分享
* */
jQuery.ajax({
    url: "http://res.wx.qq.com/open/js/jweixin-1.0.0.js",
    dataType: "script",
    cache: false
}).done(function(){
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPath = curWwwPath.substring(0, pos);
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    var basePath=localhostPath+projectName+"/";

    var shareUrl = "";
    var shareTitle = "";
    var shareDesc = "";
    var shareImg = basePath+"/res/img/ico.png";
    if(GetUrl("pid")!="" && GetUrl("pid")!=null && GetUrl("pid")!=undefined && GetUrl("pid")!="null"){
        shareUrl=basePath+"/v1/merchandise/product/wechat/"+GetUrl("pid");
    }else{
        shareUrl = basePath+"v1/user/wx/login";
    }
    $.ajax({
        type : "post",
        url: basePath+"/v1/system/wechat/jsSdkConfig",
        data: {
            url:window.location.href,
            t:new Date().getTime()
        },
        dataType:"json",
        success: function(data){
            wx.config({
                debug:false,
                appId:data.data.appid,
                timestamp:data.data.timestamp,
                nonceStr:data.data.nonceStr,
                signature:data.data.signature,
                jsApiList: [
                    'checkJsApi',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'closeWindow',
                    'hideMenuItems',
                    'hideAllNonBaseMenuItem',
                    'showMenuItems'
                ]
            });
            wx.ready(function(){
                //判断当前客户端版本是否支持指定JS接口
                wx.checkJsApi({
                    jsApiList: [
                        'checkJsApi',
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                        'closeWindow',
                        'hideMenuItems',
                        'hideAllNonBaseMenuItem',
                        'showMenuItems'
                    ],
                    success: function (res) {
                    }
                });
                wx.hideAllNonBaseMenuItem();
                setTimeout(function(){
                    wx.showMenuItems({
                        menuList: [
                            'menuItem:share:appMessage',
                            'menuItem:share:timeline'
                        ]
                    });
                    shareTitle = document.title;
                    shareDesc = document.title;
                    //分享到朋友圈
                    wx.onMenuShareTimeline({
                        title: shareTitle,
                        link: shareUrl,
                        desc: shareDesc,
                        imgUrl: shareImg,
                        trigger: function (res) {
                        },
                        success: function (res) {
                        },
                        cancel: function (res) {
                        }
                    });
                    //分享给朋友
                    wx.onMenuShareAppMessage({
                        title: shareTitle, // 分享标题
                        desc: shareDesc, // 分享描述
                        link: shareUrl, // 分享链接
                        imgUrl: shareImg, // 分享图标
                        type: 'link', // 分享类型,music、video或link，不填默认为link
                        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                        success: function () {
                        },
                        cancel: function () {
                        }
                    });

                },600);
            });
            wx.error(function (res) {
            });
        }
    });
});